package com.oss.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oss.common.BaseResponse;
import com.oss.dto.user.*;
import com.oss.entity.system.User;
import com.oss.service.system.UserService;
import com.oss.vo.user.TokenVO;
import com.oss.vo.user.UserLoginReturnVO;
import com.oss.vo.user.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author chengqiang
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    /**
     * 根据用户token获取用户信息
     * @return 返回用户信息
     */
    @PostMapping("/getUserByToken")
    public BaseResponse<UserLoginReturnVO> getUserByToken(@RequestBody GetUserByTokenDto dto) throws Exception {
        return BaseResponse.success(userService.getUserByToken(dto.getToken()));
    }

    /**
     * 根据用户ID获取用户信息
     * @param dto 入参
     * @return 返回用户信息
     */
    @PostMapping("/getUser")
    public BaseResponse<UserVO> getUser(@RequestBody GetOneUserDto dto){
        return BaseResponse.success(userService.getUserById(dto.getUserId()));
    }

    /**
     * 登录
     * @param dto 入参
     * @return 返回
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginReturnVO> login(@RequestBody @Valid LoginDto dto){
        return userService.login(dto);
    }

    /**
     * 用户分页查询
     * @param dto 分页入参
     * @return 返回用户分页信息
     */
    @PostMapping("/selectByPage")
    public BaseResponse<IPage<UserVO>> selectByPage(@RequestBody @Valid UserPageDto dto){
        return BaseResponse.success(userService.selectByPage(dto));
    }

    /**
     * 添加用户
     * @param dto 入参
     * @return 返回操作提示信息
     */
    @PostMapping("/addUser")
    public BaseResponse<String> addUser(@RequestBody @Valid UserAddDto dto){
        return userService.addUser(dto);
    }

    /**
     * 更新用户信息
     * @param dto 入参
     * @return 返回日提示信息
     */
    @PostMapping("/updateUser")
    public BaseResponse<String> updateUser(@RequestBody @Valid UserUpdateDto dto){
        return userService.updateUser(dto);
    }
    /**
     * 修改用户密码
     * @param dto 入参
     * @return 返回日提示信息
     */
    @PostMapping("/updateUserPwd")
    public BaseResponse<String> updateUserPwd(@RequestBody @Valid UserUpdatePwdDto dto){
        return BaseResponse.success(userService.updateUserPwd(dto));
    }
    /**
     * 重置用户密码
     * @param dto 入参
     * @return 返回日提示信息
     */
    @PostMapping("/resetUserPwd")
    public BaseResponse<String> resetUserPwd(@RequestBody @Valid UserResetPwdDto dto){
        return BaseResponse.success(userService.resetUserPwd(dto));
    }
    /**
     * 删除用户
     * @param dto 入参
     * @return 返回日提示信息
     */
    @PostMapping("/deleteUser")
    public BaseResponse<String> deleteUser(@RequestBody @Valid UserDeleteDto dto){
        return BaseResponse.success(userService.deleteUser(dto));
    }
    @PostMapping("/logout")
    public BaseResponse<String> logout(HttpServletRequest request){
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        return BaseResponse.success(userService.logout(token));
    }

    @PostMapping("/listAll")
    public BaseResponse<User> listAll(){
        return BaseResponse.success(userService.listAll());
    }

    @PostMapping("/uploadAvatar")
    public BaseResponse<String> uploadAvatar(MultipartFile file) throws Exception {
        return BaseResponse.success((Object)userService.uploadAvatar(file));
    }
    @PostMapping("/updateAvatar")
    public BaseResponse<String> updateAvatar(@RequestBody @Valid UserAvatarUploadDto dto) throws Exception {
        return BaseResponse.success(userService.updateAvatar(dto));
    }
    @PostMapping("/userLoginRecordsPage")
    public BaseResponse<IPage<TokenVO>> loginRecordsPage(@RequestBody @Valid TokenPageDto dto){
        return BaseResponse.success(userService.loginRecordsPage(dto));
    }

    @PostMapping("/exportUser")
    public void exportUser(@RequestBody Map map){
        userService.exportUser();
    }

}
